/**
 * Classe de gestion de l'ajout de client
* @author I.T. Works.
* @date 20/12/2011
* @version 1.0
*/

package org.itworks.biodev.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

import org.itworks.biodev.gui.panels.*;
import org.itworks.biodev.model.Customer;

public class CreateCustomerDialogView extends JDialog implements Observer{

	public final static int INIT_DAYS_COMBOBOX = 1;
	public static final int FILL_COMBOBOX_CUSTOMERS = 2;
	public static final int STATUS_IS_UPDATING = 3;
	public static final int SET_CUSTOMER = 4;
	
	private CreateCustomerDialogControler controler;
	private Customer customerToEdit = null;
	private boolean edit=false;
	
	 // Variables declaration - do not modify
    private javax.swing.JLabel addressLabel;
    private javax.swing.JTextField addressTextField;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelNewCustomerButton;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JTextField cityTextField;
    private javax.swing.JLabel customersAddDialogTitleLabel;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JTextField lastNameTextField;
    private javax.swing.JLabel mailLabel;
    private javax.swing.JTextField mailTextField;
    private javax.swing.JLabel postalCodeLabel;
    private javax.swing.JTextField postalCodeTextField;
    private javax.swing.JButton validateNewCustomerButton;
    private javax.swing.JLabel phoneNumberLabel;
    private javax.swing.JTextField phoneNumberTextField;
    // End of variables declaration
	/**
	 * Constructeur avec arguments 
	 * @param controler le controleur qui correspond a cette fenetre (view)
	 * @param customerToEdit le client à editer - Null = Creation de client
	 * 											- !=Null = Modification du client
	 */
    public CreateCustomerDialogView(CreateCustomerDialogControler controler, Customer customerToEdit) {
    	this.controler = controler;
    	
    	// S'il existe un client à modifer alors on est en mode "edit"
    	if (customerToEdit != null)
    	{
    		//Mode "Edit"
    		this.customerToEdit=customerToEdit;
    		this.edit=true;
    	}
    	else
    	{
    		//Mode "Create"
    		this.edit=false;
    	}
    	
    	this.controler.getModel().addObserver(this);
    	
    	initComponents();
    	initHandlers();
    	
    	controler.viewLoaded();
    }
    
	/**
	 * Création de la fenetre
	 */
	private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lastNameLabel = new javax.swing.JLabel();
        lastNameTextField = new javax.swing.JTextField();
        firstNameLabel = new javax.swing.JLabel();
        firstNameTextField = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        addressTextField = new javax.swing.JTextField();
        cityLabel = new javax.swing.JLabel();
        cityTextField = new javax.swing.JTextField();
        postalCodeLabel = new javax.swing.JLabel();
        postalCodeTextField = new javax.swing.JTextField();
        mailLabel = new javax.swing.JLabel();
        mailTextField = new javax.swing.JTextField();
        customersAddDialogTitleLabel = new javax.swing.JLabel();
        validateNewCustomerButton = new javax.swing.JButton();
        cancelNewCustomerButton = new javax.swing.JButton();
        phoneNumberTextField = new javax.swing.JTextField();
        phoneNumberLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        
        /*
         * Si la fenetre est ouverte pour modifier 
         * Alors les champs seront préremplis
         */
        if(this.edit == true)
        {
        	lastNameTextField.setText(this.customerToEdit.getLastName());
        	firstNameTextField.setText(this.customerToEdit.getFirstName());
        	addressTextField.setText(this.customerToEdit.getAddress());
        	cityTextField.setText(this.customerToEdit.getCity());
        	postalCodeTextField.setText(String.valueOf(this.customerToEdit.getPostalCode()));
        	mailTextField.setText(this.customerToEdit.getMail());
        	phoneNumberTextField.setText(this.customerToEdit.getPhoneNumber());

        }

        lastNameLabel.setText("Nom");
        firstNameLabel.setText("Prenom");
        addressLabel.setText("Adresse");
        cityLabel.setText("Ville");
        postalCodeLabel.setText("Code postal");
        mailLabel.setText("E-mail");

        customersAddDialogTitleLabel.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        customersAddDialogTitleLabel.setText("Gestion de client");

        validateNewCustomerButton.setText("OK");

        cancelNewCustomerButton.setText("Annuler");

        phoneNumberLabel.setText("Tel :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(customersAddDialogTitleLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lastNameLabel)
                                    .addComponent(firstNameLabel)
                                    .addComponent(addressLabel))
                                .addGap(46, 46, 46)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(firstNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                    .addComponent(lastNameTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                    .addComponent(addressTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(cityLabel)
                                .addGap(18, 18, 18)
                                .addComponent(cityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(postalCodeLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(postalCodeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(phoneNumberLabel)
                                    .addComponent(mailLabel))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(phoneNumberTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                                    .addComponent(mailTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE))))
                        .addGap(37, 37, 37)))
                .addGap(37, 37, 37))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(102, Short.MAX_VALUE)
                .addComponent(validateNewCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cancelNewCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customersAddDialogTitleLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstNameLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addressLabel)
                    .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cityLabel)
                    .addComponent(cityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(postalCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(postalCodeLabel))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phoneNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mailLabel)
                    .addComponent(mailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(validateNewCustomerButton)
                    .addComponent(cancelNewCustomerButton))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>

	/**
	 * Gestion du comportement des boutons
	 */
	private void initHandlers() {
		/*
		 * Action du bouton valider
		 */
		this.validateNewCustomerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				// TODO Auto-generated method stub
				//isOK = true;
				//On récupère les valeurs en cours
				String lastName = lastNameTextField.getText();
				String firstName = firstNameTextField.getText();
				String address = addressTextField.getText();
				String city = cityTextField.getText();
				int postalCode = Integer.parseInt(postalCodeTextField.getText());
				String mail = mailTextField.getText();
				String phoneNumber = phoneNumberTextField.getText();
				Customer customer = new Customer ( lastName, firstName, address, postalCode,  city, mail, phoneNumber);

				// Si on est en mode "edit" on update le client
				if (edit==true)
				{
					customer.setId(customerToEdit.getId());
					controler.validateNewCustomerButtonActionPerformed(actionEvent, customer, edit);
				}
				else
				{
					controler.validateNewCustomerButtonActionPerformed(actionEvent, customer, edit);
				}
				setVisible(false);
			}
    		
    	});
		
	
		/*
		 * Action du bouton annuler
		 */
    	this.cancelNewCustomerButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				// TODO Auto-generated method stub
				setVisible(false);
			}
    		
    	});

	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}
}
